import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class JedisDemoSet {
    public static void test1(Jedis jedis) {
        System.out.println("sadd 和smembers");
        jedis.flushAll();
        jedis.sadd("key", "111","2222");
        //这里由于key不能重复，所以要用set集合表示
        Set<String> smembers = jedis.smembers("key");
        System.out.println(smembers);
    }
    public static void test2(Jedis jedis) {
        System.out.println("sismember 的使用");
        jedis.flushAll();
        jedis.sadd("key", "111","2222");
        Boolean b = jedis.sismember("key", "111");
        Boolean b2 = jedis.sismember("key","444444444");
        System.out.println(b);
        System.out.println(b2);
    }
    public static void test3(Jedis jedis) {
        System.out.println("scard 的使用");
        jedis.flushAll();
        jedis.sadd("key", "111","2222","333333333","4444444");
        System.out.println(jedis.scard("key"));
    }
    public static void test4(Jedis jedis) {
        System.out.println("spop");
        jedis.flushAll();
        jedis.sadd("key", "111","2222","333333333","444444444");
        String result=jedis.spop("key");
        //这里是随机删除的结果
        System.out.println("删除的结果："+result);
    }
    public static void test5(Jedis jedis) {
        System.out.println("sinter");
        jedis.flushAll();
        jedis.sadd("key", "111","2222");
        jedis.sadd("key1", "333333333","444444444","111");
        Set<String> sinter = jedis.sinter("key1", "key");
        System.out.println(sinter);
        System.out.println(sinter.size());
    }
    public static void test6(Jedis jedis) {
        System.out.println("sinterstore");
        jedis.flushAll();
        jedis.sadd("key", "111","2222");
        jedis.sadd("key1", "333333333","444444444","111");
        jedis.sinterstore("key", "key1", "key");
        System.out.println(jedis.smembers("key"));
    }
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://localhost:8888");
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.auth("maihao1122");
            //test1(jedis);
            //test2(jedis);
            //test3(jedis);
            //test4(jedis);
            //test5(jedis);
            test6(jedis);
        }
    }
}
